//
//  TestPoiSearchViewController.m
//  百度地图
//
//  Created by ljm on 2017/7/7.
//  Copyright © 2017年 ljm. All rights reserved.
//

#import "TestPoiSearchViewController.h"

@interface TestPoiSearchViewController ()
<BMKLocationServiceDelegate,BMKMapViewDelegate>
{
    BMKLocationService *_locationServer;
    BMKMapView *_mapView;
    BMKUserLocation *_userLocation;
}

@end

@implementation TestPoiSearchViewController

- (void)viewWillAppear:(BOOL)animated
{
    [_mapView viewWillAppear];
    _mapView.delegate = self;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [_mapView viewWillDisappear];
    _mapView.delegate = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
 
    self.title = @"定位测试";
    
    [self loadMapView];
    [self loadLocation];
}

#pragma mark -- 加载地图
- (void) loadMapView
{
//    _mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 64, screenWidth, screenHeight - 64)];
    _mapView = [[BMKMapView alloc] initWithFrame:self.view.frame];
    _mapView.mapType = BMKMapTypeStandard;
    _mapView.logoPosition = BMKLogoPositionRightBottom;
    _mapView.zoomLevel = 16.f;
    [self.view addSubview:_mapView];
}

#pragma mark -- 定位
- (void) loadLocation
{
    
    _locationServer = [[BMKLocationService alloc] init];
    _locationServer.delegate = self;
    [_locationServer startUserLocationService];
    
}

#pragma mark -- 要定位到当前位置，就必须实现这个代理
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    BMKCoordinateRegion region;
    region.center.latitude = userLocation.location.coordinate.latitude;
    region.center.longitude = userLocation.location.coordinate.longitude;
    
    region.span.latitudeDelta = 0;
    region.span.longitudeDelta = 0;
    if (_mapView)
    {
        _mapView.region = region;
        
    }

    [_locationServer stopUserLocationService];//定位完成停止位置更新
    _userLocation = [[BMKUserLocation alloc] init];
    _userLocation = userLocation;
    
    [self setupAnnoation];
//    //添加当前位置的标注
//    CLLocationCoordinate2D coord;
//    coord.latitude = userLocation.location.coordinate.latitude;
//    coord.longitude = userLocation.location.coordinate.longitude;
//    BMKPointAnnotation *_pointAnnotation = [[BMKPointAnnotation alloc] init];
//    _pointAnnotation.coordinate = coord;
//    
//    CLLocationCoordinate2D pt = CLLocationCoordinate2DMake(coord.latitude, coord.longitude);
//    
//    dispatch_async(dispatch_get_main_queue(), ^{
//        [_mapView removeOverlays:_mapView.overlays];
//        [_mapView setCenterCoordinate:coord animated:true];
//        [_mapView addAnnotation:_pointAnnotation];
//        
//    });
}

#pragma mark -- 大头针
- (void) setupAnnoation
{
    CLLocationCoordinate2D pt = CLLocationCoordinate2DMake(_userLocation.location.coordinate.latitude, _userLocation.location.coordinate.longitude);
    BMKPointAnnotation *_pointAnnoation = [[BMKPointAnnotation alloc] init];
    _pointAnnoation.coordinate = pt;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [_mapView addAnnotation:_pointAnnoation];
    });
}


@end
